#include <reg52.h>   
#include <intrins.h>

#define Addr_Decode_Mode  0x09 //译码模式寄存器地址
#define Addr_Intensity 	  0x0a //亮度控制寄存器地址
#define Addr_Scan_Limit   0x0b //扫描控制寄存器地址
#define Addr_Shutdowm     0x0c //掉电模式寄存器地址
#define Addr_Display_Test 0x0f //显示检测寄存器地址
#define Addr_Digit1       0x01 //数码管位1
#define Addr_Digit2       0x02 //数码管位2
#define Addr_Digit3       0x03 //数码管位3
#define Addr_Digit4       0x04 //数码管位4
#define Addr_Digit5       0x05 //数码管位5
#define Addr_Digit6       0x06 //数码管位6
#define Addr_Digit7       0x07 //数码管位7
#define Addr_Digit8       0x08 //数码管位8


//定义Max7219端口            
sbit Max7219_pinDIN = P0^0;  
sbit Max7219_pinCS  = P0^1;
sbit Max7219_pinCLK = P0^2;

void WriteMax7219Byte(unsigned char DATA) {
    unsigned char i;
    for (i = 8; i >= 1; i--) {
        Max7219_pinCLK = 0;
        Max7219_pinDIN = DATA & 0x80;
        DATA = DATA << 1;
        Max7219_pinCLK = 1;
    }
}

void WriteMax7219(unsigned char address, unsigned char dat) {
    Max7219_pinCS = 0;
    WriteMax7219Byte(address);           //写入地址，即数码管编号
    WriteMax7219Byte(dat);               //写入数据，即数码管显示数字 
    Max7219_pinCS = 1;
}

void Max7219_Init()
{
	unsigned char addr,i;

	WriteMax7219(Addr_Decode_Mode,  0x00); //译码方式: 不编码
    WriteMax7219(Addr_Intensity,    0x00); //亮度调节: 范围[0x00 ~ 0x0F]
    WriteMax7219(Addr_Scan_Limit,   0x07); //扫描界限: 8个数码管显示
    WriteMax7219(Addr_Shutdowm,     0x01); //关闭模式: 0:关闭 1:开启
    WriteMax7219(Addr_Display_Test, 0x00); //显示测试：1；测试结束，正常显示：0
	
	addr = 0x00;
    for (i = 0; i < 8; i++) {
        addr += 1;
        WriteMax7219(addr, 0x00);  //全部位默认不展示
    }
}